﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class CategoryManager : BusinessLogic
    {
        public CATEGORIES Load(int ID)
        {
            return (CATEGORIES)ObjectRow.Load(typeof(CATEGORIES), string.Format("this.ID={0}", ID)); 
        }

        public CATEGORIES LoadID(string Code)
        {
            return (CATEGORIES)ObjectRow.Load(typeof(CATEGORIES), string.Format("this.CategoryCode='{0}'", Code));
        }

        public CATEGORIES Load(string code, decimal domainID, decimal ID)
        {
            return (CATEGORIES)ObjectRow.Load(typeof(CATEGORIES), string.Format("this.CategoryCode='{0}' and this.DOMAINID={1} and this.ID<>'{2}'", code, domainID, ID));
        }


        public CATEGORIES LoadName(string Name, decimal DomainID)
        {
            return (CATEGORIES)ObjectRow.Load(typeof(CATEGORIES), string.Format("this.CategoryName='{0}' AND this.DOMAINID={1}", Name, DomainID));
        }

        public CATEGORIES LoadDomianID(string Code, decimal DomainID)
        {
            return (CATEGORIES)ObjectRow.Load(typeof(CATEGORIES), string.Format("this.CategoryCode='{0}' AND this.DOMAINID={1}", Code, DomainID));
        }

        public ObjectTable GetCategoryTable(string whereStr,decimal domainid)
        {
           
            if (domainid  > 0)
            {
               whereStr= whereStr+string.Format(" and this.DOMAINID='{0}'", domainid);
            }
            return new ObjectTable(typeof(CATEGORIES), whereStr);
        }

        //绑定品类
        public static ObjectTable GetCategories(decimal domainid)
        {
            return new ObjectTable(typeof(CATEGORIES), "this.DOMAINID='" + domainid + "' and this.ACTIVE='1'");
        }

        /// <summary>
        /// 保存品类表信息
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public bool Save(CATEGORIES category)
        {
            try
            {
                if (this.Load(category.CategoryCode, category.DOMAINID, category.ID) != null)
                {
                    throw new Exception("代码已存在! ");
                }
               
                category.Save();
                
                return true;
            }
            catch (Exception ex)
            {
                this.message = ex.Message;
                throw ex;
            }
        }
    }
}
